package com.rams.jhms.gui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import net.miginfocom.layout.AC;
import net.miginfocom.layout.CC;
import net.miginfocom.layout.LC;
import net.miginfocom.swing.MigLayout;

import com.rams.jhms.Config;
import com.rams.jhms.fs.FSFile;

public class FileEditForm extends JDialog {
	/**
	 * 
	 */
	private static final long serialVersionUID = 2719968956814599658L;
	private static JDialog form;
	private static JTabbedPane mainTabs = new JTabbedPane();
	private static JPanel fileInfo = new JPanel();
	private static JPanel v1tag = new JPanel();
	private static JPanel v2tag = new JPanel();
	
	// File info panel components
	private static JLabel title = new JLabel(Config.local.fileEditStringTitle());
	private static JLabel fTitle = new JLabel();
	private static JLabel artist = new JLabel(Config.local.fileEditStringArtist());
	private static JLabel fArtist = new JLabel();
	private static JLabel album = new JLabel(Config.local.fileEditStringAlbum());
	private static JLabel fAlbum = new JLabel();
	private static JLabel genre = new JLabel(Config.local.fileEditStringGenre());
	private static JLabel fGenre = new JLabel();
	private static JLabel year = new JLabel(Config.local.fileEditStringYear());
	private static JLabel fYear = new JLabel();
	private static JLabel format = new JLabel(Config.local.fileEditStringFormat());
	private static JLabel fFormat = new JLabel();
	private static JLabel duration = new JLabel(Config.local.fileEditStringDuration());
	private static JLabel fDuration = new JLabel();
	private static JLabel quality = new JLabel(Config.local.fileEditStringQuality());
	private static JLabel fQuality = new JLabel();
	private static JLabel size = new JLabel(Config.local.fileEditStringSize());
	private static JLabel fSize = new JLabel();
	// End File info panel components
	
	// ID3V1 panel
	private static JLabel v1Title = new JLabel(Config.local.fileEditStringTitle());
	private static JLabel v1Artist = new JLabel(Config.local.fileEditStringArtist());
	private static JLabel v1Album = new JLabel(Config.local.fileEditStringAlbum());
	private static JLabel v1Genre = new JLabel(Config.local.fileEditStringGenre());
	private static JLabel v1Year = new JLabel(Config.local.fileEditStringYear());
	private static JLabel v1TrackNo = new JLabel(Config.local.fileEditStringTrackNo());
	private static JLabel v1DiskNo = new JLabel(Config.local.fileEditStringDiskNo());
	private static JTextField v1fTitle = new JTextField();
	private static JTextField v1fArtist = new JTextField();
	private static JTextField v1fAlbum = new JTextField();
	private static JTextField v1fGenre = new JTextField();
	private static JTextField v1fYear = new JTextField();
	private static JTextField v1fTrackNo = new JTextField();
	private static JTextField v1fDiskNo = new JTextField();
	// TODO: add comments field
	// End ID3V1 panel
	
	// ID3V2 panel
	private static JLabel v2Title = new JLabel(Config.local.fileEditStringTitle());
	private static JLabel v2Artist = new JLabel(Config.local.fileEditStringArtist());
	private static JLabel v2Album = new JLabel(Config.local.fileEditStringAlbum());
	private static JLabel v2Genre = new JLabel(Config.local.fileEditStringGenre());
	private static JLabel v2Year = new JLabel(Config.local.fileEditStringYear());
	private static JLabel v2TrackNo = new JLabel(Config.local.fileEditStringTrackNo());
	private static JLabel v2DiskNo = new JLabel(Config.local.fileEditStringDiskNo());
	private static JLabel v2URL = new JLabel(Config.local.fileEditStringUrl());
	private static JLabel v2Composer = new JLabel(Config.local.fileEditStringComposer());
	private static JLabel v2Ensemble = new JLabel(Config.local.fileEditStringEnsemble());
	private static JLabel v2Copyright = new JLabel(Config.local.fileEditStringCopyright());
	private static JLabel v2Comment = new JLabel(Config.local.fileEditStringComment());
	private static JTextField v2fTitle = new JTextField();
	private static JTextField v2fArtist = new JTextField();
	private static JTextField v2fAlbum = new JTextField();
	private static JTextField v2fGenre = new JTextField();
	private static JTextField v2fYear = new JTextField();
	private static JTextField v2fTrackNo = new JTextField();
	private static JTextField v2fDiskNo = new JTextField();
	private static JTextField v2fURL = new JTextField();
	private static JTextField v2fComposer = new JTextField();
	private static JTextField v2fEnsemble = new JTextField();
	private static JTextField v2fCopyright = new JTextField();
	private static JTextArea v2fComment = new JTextArea();
	// End ID3V2 panel
	
	private static Icon confirmYellow = new ImageIcon("imgs/confirm_yellow.jpg");
	private static Icon confirmGreen = new ImageIcon("imgs/confirm_green.jpg");
	private static Icon confirmRed = new ImageIcon("imgs/confirm_red.jpg");
	private static JButton artistConfirmButton = new JButton();
	private static JButton albumConfirmButton = new JButton();

	private static JSeparator sep = new JSeparator();
	
	private FileEditForm() {
		this.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
		this.setModal(true);
		
		artistConfirmButton.setLayout(null);
		Dimension dsize = new Dimension(18, 18);
		artistConfirmButton.setSize(dsize);
		artistConfirmButton.setPreferredSize(dsize);
		artistConfirmButton.setMinimumSize(dsize);
		artistConfirmButton.setMaximumSize(dsize);
		artistConfirmButton.setActionCommand("check_artist");
		artistConfirmButton.addActionListener(new btnCheckAction());

		albumConfirmButton.setSize(dsize);
		albumConfirmButton.setPreferredSize(dsize);
		albumConfirmButton.setMinimumSize(dsize);
		albumConfirmButton.setMaximumSize(dsize);
		albumConfirmButton.setActionCommand("check_album");
		albumConfirmButton.addActionListener(new btnCheckAction());

		// File info form
		MigLayout ml = new MigLayout(new LC().debug(Config.layoutDebug).noGrid(),
					                 new AC().grow().shrink(),
					                 new AC());
		
		fileInfo.setLayout(ml);
		
		fileInfo.add(title, new CC().width("50"));
		fileInfo.add(fTitle, new CC().grow().minWidth("110"));
		fileInfo.add(artist, new CC().newline().width("50"));
		fileInfo.add(fArtist, new CC().grow().minWidth("110"));
		fileInfo.add(album, new CC().newline().width("50"));
		fileInfo.add(fAlbum, new CC().grow().minWidth("110"));
		fileInfo.add(genre, new CC().newline().width("50"));
		fileInfo.add(fGenre, new CC().grow().minWidth("110"));
		fileInfo.add(year, new CC().newline().width("50"));
		fileInfo.add(fYear, new CC().grow().minWidth("110"));
		fileInfo.add(sep, new CC().grow().newline());
		fileInfo.add(format, new CC().newline().width("50"));
		fileInfo.add(fFormat, new CC().grow().minWidth("110"));
		fileInfo.add(duration, new CC().newline().width("50"));
		fileInfo.add(fDuration, new CC().grow().minWidth("110"));
		fileInfo.add(quality, new CC().newline().width("50"));
		fileInfo.add(fQuality, new CC().grow().minWidth("110"));
		fileInfo.add(size, new CC().newline().width("50"));
		fileInfo.add(fSize, new CC().grow().minWidth("110"));
		// End File info form

		// id3v1 tags panel
		MigLayout ml1 = new MigLayout(new LC().debug(Config.layoutDebug).noGrid(),
                new AC().grow().shrink(),
                new AC());

		v1tag.setLayout(ml1);
		
		v1tag.add(v1TrackNo, 	new CC().width("50"));
		v1tag.add(v1fTrackNo,	new CC().grow().minWidth("20"));
		v1tag.add(v1DiskNo, 	new CC().width("40").gapX("15", null));
		v1tag.add(v1fDiskNo, 	new CC().grow().minWidth("20"));
		v1tag.add(v1Title, 		new CC().width("50").newline());
		v1tag.add(v1fTitle, 	new CC().grow().minWidth("110"));
		v1tag.add(v1Artist, 	new CC().newline().width("50"));
		v1tag.add(v1fArtist, 	new CC().grow().minWidth("110"));
		v1tag.add(v1Album, 		new CC().newline().width("50"));
		v1tag.add(v1fAlbum, 	new CC().grow().minWidth("110"));
		v1tag.add(v1Genre, 		new CC().newline().width("50"));
		v1tag.add(v1fGenre, 	new CC().grow().minWidth("110"));
		v1tag.add(v1Year, 		new CC().newline().width("50"));
		v1tag.add(v1fYear, 		new CC().grow().minWidth("110"));
		// End id3v1 tags panel
		
		// id3v2 tags panel
		MigLayout ml2 = new MigLayout(new LC().debug(Config.layoutDebug),
									  new AC().grow(),
					                  new AC());
		v2tag.setLayout(ml2);
		
		v2tag.add(v2TrackNo, 	new CC().cell(0, 0).width("50"));
		v2tag.add(v2fTrackNo, 	new CC().cell(1, 0).grow());
		v2tag.add(v2DiskNo, 	new CC().cell(1, 0));
		v2tag.add(v2fDiskNo, 	new CC().cell(1, 0).grow());
		v2tag.add(v2Comment, 	new CC().cell(2, 0));
		v2tag.add(v2fComment, 	new CC().cell(2, 1).spanY().grow());
		v2tag.add(v2Title, 		new CC().cell(0, 1));
		v2tag.add(v2fTitle, 	new CC().cell(1, 1).grow());
		v2tag.add(v2Artist, 	new CC().cell(0, 2));
		v2tag.add(v2fArtist, 	new CC().cell(1, 2).grow());
		v2tag.add(artistConfirmButton, new CC().cell(1, 2));
		v2tag.add(v2Album, 		new CC().cell(0, 3));
		v2tag.add(v2fAlbum, 	new CC().cell(1, 3).grow());
		v2tag.add(albumConfirmButton, new CC().cell(1, 3));
		v2tag.add(v2Genre, 		new CC().cell(0, 4));
		v2tag.add(v2fGenre, 	new CC().cell(1, 4).grow());
		v2tag.add(v2Year, 		new CC().cell(0, 5));
		v2tag.add(v2fYear, 		new CC().cell(1, 5).grow());
		v2tag.add(v2URL, 		new CC().cell(0, 6));
		v2tag.add(v2fURL, 		new CC().cell(1, 6).grow());
		v2tag.add(v2Composer, 	new CC().cell(0, 7));
		v2tag.add(v2fComposer, 	new CC().cell(1, 7).grow());
		v2tag.add(v2Ensemble, 	new CC().cell(0, 8));
		v2tag.add(v2fEnsemble, 	new CC().cell(1, 8).grow());
		v2tag.add(v2Copyright, 	new CC().cell(0, 9));
		v2tag.add(v2fCopyright, new CC().cell(1, 9).grow());
		// End id3v2 tags panel
		
		mainTabs.addTab("File Info", fileInfo);
		mainTabs.addTab("ID3V1", v1tag);
		mainTabs.addTab("ID3V2", v2tag);
		
		this.setContentPane(mainTabs);
		this.setMinimumSize(new Dimension(400, 270));
		this.setResizable(false);
		
		this.pack();
		this.setLocationRelativeTo(null);
	}
	
	public static void showForm(FSFile file) {
		if (form == null)
			form = new FileEditForm();
		
		fillForm(file);
		mainTabs.setSelectedIndex(0);
		
		form.setVisible(true);
	}
	
	public static void hideForm() {
		form.setVisible(false);
		clearForm();
	}
	
	private static void fillForm(FSFile file) {
		fillFileInfo(file);
		fillID3V1Tags(file);
		fillID3V2Tags(file);
		checkInputData();
	}

	private static void checkInputData() {
		artistConfirmButton.setIcon(confirmRed);
		albumConfirmButton.setIcon(confirmRed);
	}

	private static void fillID3V2Tags(FSFile file) {
		if (!file.hasID3v2Tag()) {
			return;
		}
		v2fTitle.setText(file.getV2Title());
		v2fArtist.setText(file.getV2Artist());
		v2fAlbum.setText(file.getV2Album());
		v2fGenre.setText(file.getV2Genre());
		v2fYear.setText(file.getV2Year());
		v2fTrackNo.setText(file.getV2TrackNo());
		//v2fDiskNo.setText(file.getV2DiskNo());
	}

	private static void fillID3V1Tags(FSFile file) {
		if (!file.hasID3v1Tag()) {
			return;
		}
		v1fTitle.setText(file.getV1Title());
		v1fArtist.setText(file.getV1Artist());
		v1fAlbum.setText(file.getV1Album());
		v1fGenre.setText(file.getV1Genre());
		v1fYear.setText(file.getV1Year());
		v1fTrackNo.setText(file.getV1TrackNo());
		v1fDiskNo.setText(file.getV1DiskNo());
	}

	private static void fillFileInfo(FSFile file) {
		fTitle.setText(file.getTitle());
		fArtist.setText(file.getArtist());
		fAlbum.setText(file.getAlbum());
		fGenre.setText(file.getGanre());
		fYear.setText(file.getYear());
		fSize.setText(file.getFormatedSize());
		fDuration.setText(file.getFormatedTrackLength());
		fQuality.setText(file.getTrackQuality());
		fFormat.setText(file.getFileFormat());
	}
	
	private static void clearForm() {
		fTitle.setText("");
		fArtist.setText("");
		fAlbum.setText("");
		fGenre.setText("");
		fYear.setText("");
		fSize.setText("");
		fDuration.setText("");
		fQuality.setText("");
		fFormat.setText("");
		
		v1fTitle.setText("");
		v1fArtist.setText("");
		v1fAlbum.setText("");
		v1fGenre.setText("");
		v1fYear.setText("");
		
		v2fTitle.setText("");
		v2fArtist.setText("");
		v2fAlbum.setText("");
		v2fGenre.setText("");
		v2fYear.setText("");
		v2fTrackNo.setText("");
		v2fDiskNo.setText("");
		v2fURL.setText("");
		v2fComposer.setText("");
		v2fEnsemble.setText("");
		v2fCopyright.setText("");
		v2fComment.setText("");
	}

	private class btnCheckAction implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			String command = e.getActionCommand();
			if ("check_artist".equals(command)) {
				new AddArtistForm(form, v2fArtist.getText()).setVisible(true);
			} else if("check_album".equals(command)) {
				albumConfirmButton.setIcon(confirmYellow);
			}
		}
	}
}
